﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyBlog.Common;
using MyBlog.Model;
using Webdiyer.WebControls.Mvc;

namespace MyBlog.Web.Controllers
{
    public class MoodController : BaseController
    {
        [TrackRequest]
        public ActionResult Index(string username)
        {
            var all = XiaoBoKe.MoodService.GetAll(new MoodModel { AccountId = CurBlogUserID });
            ViewBag.TotalPage = all.Count() % 15 == 0 ? (all.Count / 15) : (all.Count / 15 + 1);
            ViewBag.total = all.Count;
            ViewBag.username = username;
            ViewBag.CanEdit = CanEdit;
            ViewBag.BlogUserName = CurBlogUserName;
            return View();
        }

        public PartialViewResult _MoodAjaxList(string username, int page = 1)
        {
            var moodlist = XiaoBoKe.MoodService.GetAll(new MoodModel { AccountId = username }).OrderByDescending(x => x.CreateTime).ToPagedList(page, 15);
            ViewBag.CanEdit = CanEdit;
            ViewBag.blogUid = CurBlogUserID;
            return PartialView(moodlist);
        }

        [HttpPost]
        [Authenticate]
        public JsonResult Release(MoodModel model)
        {
            if (model.ID == 0)
            {
                XiaoBoKe.MoodService.Add(model);
                base.AddNotice(UserSession.AccountId, NoticeEnum.AddMood, null, model.Content);
            }
            else
            {
                XiaoBoKe.MoodService.Update(model);
            }
            return Json(new JsonData { State = AjaxState.Success });
        }

        [Authenticate(false)]
        [HttpPost]
        public JsonResult Agree(int id)
        {
            var model = XiaoBoKe.MoodService.Get(id);
            if (string.IsNullOrWhiteSpace(model.AgreeUsers) || (!string.IsNullOrWhiteSpace(model.AgreeUsers) && !model.AgreeUsers.Contains(UserSession.AccountId)))
            {
                XiaoBoKe.MoodService.AddAgree(id, UserSession.AccountId);
                return Json(new JsonData { State = AjaxState.Success });
            }
            else
            {
                return Json(new JsonData { State = AjaxState.Fail, Message = "你已经点过赞啦！" });
            }
        }

        [Authenticate(false)]
        [HttpPost]
        public JsonResult AgreeReply(int id)
        {
            var model = XiaoBoKe.MoodCommentService.Get(id);
            if (string.IsNullOrWhiteSpace(model.AgreeUsers) || (!string.IsNullOrWhiteSpace(model.AgreeUsers) && !model.AgreeUsers.Contains(UserSession.AccountId)))
            {
                XiaoBoKe.MoodCommentService.AddAgree(id, UserSession.AccountId);
                return Json(new JsonData { State = AjaxState.Success });
            }
            else
            {
                return Json(new JsonData { State = AjaxState.Fail, Message = "你已经点过赞啦！" });
            }
        }

        [HttpPost]
        [Authenticate(false)]
        public JsonResult AddComment(int id, string content)
        {
            var comment = new MoodCommentModel { MoodId = id, Content = content, CommentUid = UserSession.AccountId };
            XiaoBoKe.MoodCommentService.Add(comment);
            try
            {
                var item = XiaoBoKe.MoodService.Get(id);
                if (!item.AccountId.Same(UserSession.AccountId))
                {
                    var notice = WebCache.GetNickName(UserSession.AccountId) + "评论了你的说说：" + content.StripHTML();
                    EmailHelper.SendMail(WebCache.GetAccount(item.AccountId).Email, "小博客说说评论通知", notice);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("发送说说评论通知出错：" + ex.Message);
            }
            return Json(new JsonData { State = AjaxState.Success });
        }

        [HttpPost]
        [Authenticate(false)]
        public JsonResult DeleteMood(int id)
        {
            var mood = XiaoBoKe.MoodService.Get(id);
            if (mood.MoodCommentModels.Count > 0)
            {
                foreach (var item in mood.MoodCommentModels)
                {
                    XiaoBoKe.MoodCommentService.Delete(item.ID);
                }
            }
            XiaoBoKe.MoodService.Delete(id);
            return Json(new JsonData { State = AjaxState.Success });
        }

        [HttpPost]
        [Authenticate(false)]
        public JsonResult DelComment(int id)
        {
            XiaoBoKe.MoodCommentService.Delete(id);
            return Json(new JsonData { State = AjaxState.Success });
        }

        public PartialViewResult _MoodEdit(int id)
        {
            var model = XiaoBoKe.MoodService.Get(id);
            return PartialView(model);
        }
    }
}
